package my.service.Impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import my.utils.DBUtils;
import my.bean.Admin;
import my.dao.IAdminDao;
import my.dao.Impl.IAdminDaoImpl;
import my.service.IAdminService;

/**
 * 业务实现类,通过调用数据访问层代码,实现数据处理功能
 * 
 * @author slim
 *
 */
public class IAdminServiceImpl implements IAdminService {

	private IAdminDao adminDao;

	@Override
	public int add(Admin admin) throws SQLException {
		Connection conn = null;
		int record = 0;

		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			adminDao = new IAdminDaoImpl(conn);
			record = adminDao.add(admin);
			conn.commit();
			return record;
		} catch (SQLException e) {
			System.out.println("发生错误!尝试进行事务回滚!");
			try {
				conn.rollback();
				System.out.println("事务回滚成功!");
			} catch (SQLException e1) {
				System.out.println("事务回滚失败!");
			}
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e2) {
				e2.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public int deleteAdminById(int userId) throws SQLException {
		Connection conn = null;
		int record = 0;

		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			adminDao = new IAdminDaoImpl(conn);
			record = adminDao.deleteAdminById(userId);
			conn.commit();
			return record;
		} catch (SQLException e) {
			System.out.println("发生错误!尝试进行事务回滚!");
			try {
				conn.rollback();
				System.out.println("事务回滚成功!");
			} catch (SQLException e1) {
				System.out.println("事务回滚失败!");
			}
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e2) {
				e2.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}

	}

	@Override
	public int modify(Admin admin) throws SQLException {
		Connection conn = null;
		int record = 0;

		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			adminDao = new IAdminDaoImpl(conn);
			record = adminDao.modify(admin);
			conn.commit();
			return record;
		} catch (SQLException e) {
			System.out.println("发生错误!尝试进行事务回滚!");
			try {
				conn.rollback();
				System.out.println("事务回滚成功!");
			} catch (SQLException e1) {
				System.out.println("事务回滚失败!");
			}
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e2) {
				e2.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}

	}

	@Override
	public int modifyPassword(Admin admin) throws SQLException {
		Connection conn = null;
		int record = 0;

		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			adminDao = new IAdminDaoImpl(conn);
			record = adminDao.modifyPassword(admin);
			conn.commit();
			return record;
		} catch (SQLException e) {
			System.out.println("发生错误!尝试进行事务回滚!");
			try {
				conn.rollback();
				System.out.println("事务回滚成功!");
			} catch (SQLException e1) {
				System.out.println("事务回滚失败!");
			}
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e2) {
				e2.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}

	}

	@Override
	public List<Admin> findAll() {
		Connection conn = null;
		List<Admin> list = null;
		try {
			conn = DBUtils.getConnection();
			adminDao = new IAdminDaoImpl(conn);
			list = adminDao.findAll();
			return list;
		} finally {
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public Admin findByAdminAccount(String account) {
		Connection conn = null;
		Admin admin = null;
		try {
			conn = DBUtils.getConnection();
			adminDao = new IAdminDaoImpl(conn);
			admin = adminDao.findByAdminAccount(account);
			return admin;
		} finally {
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public Admin findByAdminId(int adminId) {
		Connection conn = null;
		Admin admin = null;
		try {
			conn = DBUtils.getConnection();
			adminDao = new IAdminDaoImpl(conn);
			admin = adminDao.findByAdminId(adminId);
			return admin;
		} finally {
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public Admin findByAccountAndPassword(String account, String password) {
		Connection conn = null;
		Admin admin = null;

		try {
			conn = DBUtils.getConnection();
			adminDao = new IAdminDaoImpl(conn);
			admin = adminDao.findByAccountAndPassword(account, password);
			return admin;
		} finally {
			DBUtils.close(conn, null, null);
		}
	}
	
}
